<!DOCTYPE html>
<html>

<head>
    <meta charset="utf-8">
    <meta name="viewport"
        content="maximum-scale=1.0,minimum-scale=1.0,user-scalable=0,width=device-width,initial-scale=1.0" />
    <meta name="format-detection" content="telephone=no,email=no,date=no,address=no">
    <link rel="stylesheet" type="text/css" href="../css/aui.2.0.css" />
</head>

<body>
    <header class="aui-bar aui-bar-nav" style="background-color: #03a9f4;position:fixed;" id="header">
        <a class="aui-pull-left aui-btn" tapmode onclick="closeWin();">
            <span class="aui-iconfont aui-icon-left"></span>
        </a>
        <div class="aui-title" id="title">
            收货验证明细
        </div>
    </header>
    <div class="aui-content aui-margin-b-15" style="margin-top:60px;">
        <ul class="aui-list aui-form-list">
            <li class="aui-list-item" style="display:none">
                <div class="aui-list-item-inner">
                    <div class="aui-list-item-label">条码内容表ID</div>
                    <div class="aui-list-item-input">
                        <input type="text" id="id" disabled="true">
                    </div>
                </div>
            </li>
            
            <li class="aui-list-item">
                <div class="aui-list-item-inner">
                    <div class="aui-list-item-label">发货单号</div>
                    <div class="aui-list-item-input">
                        <input type="text" id="consignment" disabled="true">
                    </div>
                </div>
            </li>
            <li class="aui-list-item">
                <div class="aui-list-item-inner">
                    <div class="aui-list-item-label">箱号</div>
                    <div class="aui-list-item-input">
                        <input type="text" id="boxnum" disabled="true">
                    </div>
                </div>
            </li>
            <li class="aui-list-item">
                <div class="aui-list-item-inner">
                    <div class="aui-list-item-label">物料号</div>
                    <div class="aui-list-item-input">
                        <input type="text" id="mrlCode" disabled="true">
                    </div>
                </div>
            </li>
            <li class="aui-list-item">
                <div class="aui-list-item-inner">
                    <div class="aui-list-item-label">数量</div>
                    <div class="aui-list-item-input">
                        <input type="text" id="qty">
                    </div>
                </div>
            </li>
            <li class="aui-list-item">
                <div class="aui-list-item-inner">
                    <div class="aui-list-item-label">批次号</div>
                    <div class="aui-list-item-input">
                        <input type="text" id="lotCode">
                    </div>
                </div>
            </li>
            <li class="aui-list-item">
                <div class="aui-list-item-inner">
                    <div class="aui-list-item-label" style="font-size: 14px;">检测状态</div>
                    <div class="aui-list-item-input">
                        <select id="gyStatus" disabled="true">
                        </select>
                    </div>
                </div>
            </li>
            <li class="aui-list-item">
                <div class="aui-list-item-inner">
                    <div class="aui-list-item-label" style="font-size: 14px;">检测结果</div>
                    <div class="aui-list-item-input">
                        <select id="gyCheck" onchange="onChangeGyCheck()">
                        </select>
                    </div>
                </div>
            </li>
            <li class="aui-list-item">
                <div class="aui-list-item-inner">
                    <div class="aui-list-item-label" style="font-size: 14px;">新箱号</div>
                    <input type="text" class="aui-input" placeholder="请 扫描/输入 箱号" style="width: 80%;font-size: 14px;"
                        id="newBoxnum" disabled="true">
                    <i class="aui-input-addon aui-iconfont aui-icon-scan " style="font-size: 1.6rem"
                        onclick="toScanner();" id="scanner" style="display:none"></i>
                </div>
            </li>
            <li class="aui-list-item" style="display:none">
                <div class="aui-list-item-inner">
                    <div class="aui-list-item-label">新条码</div>
                    <div class="aui-list-item-input">
                        <input type="text" id="tm">
                    </div>
                </div>
            </li>
        </ul>
        <div style="width:100%;margin-top:2%;"></div>
        <div class="aui-btn aui-btn-info" style="width:40%;margin-left:5%;" onclick="replace()" id="replace">替换箱码</div>
        <div class="aui-btn aui-btn-info" style="width:40%;margin-left:5%;" onclick="save()" id="save">保存</div>
    </div>
</body>
<script type="text/javascript" src="../script/api.js"></script>
<script type="text/javascript" src="../script/jquery-1.8.2.min.js"></script>
<script type="text/javascript" src="../script/public.js"></script>
<script type="text/javascript">
    //初始化查询明细数据和下拉框下拉框
    apiready = function () {

        //条码表ID（SS_TMCONTENT）
        var id = api.pageParam.id;
        //3:放行；4：退货
        var flag = api.pageParam.gyStatus;
        doInit(id, flag);
    }

    //初始化查询
    function doInit(id, flag) {
        var un = localStorage.getItem('un');
        var serverUrl = localStorage.getItem('serverUrl');
        var url = serverUrl + "/app/getConsignmentInput";
        var data = {
            values: { ////以表单方式提交参数（JSON对象）
                id: id,
                appVersion: api.appVersion,
                un: un
            }
        };
        api.ajax({
            url: url,
            method: 'post',
            dataType: 'json',
            timeout: 30,
            returnAll: false,
            data: data
        }, function (ret, err) {
            if (ret) {
                var data = JSON.stringify(ret);
                var myobj = eval("(" + data + ")");
                if (myobj.code == "0") {

                    //id赋值
                $("#id").val(id);
                //发货单号
                $("#consignment").val(myobj.consignment);
                //箱号
                $("#boxnum").val(myobj.boxnum);
                //物料号
                $("#mrlCode").val(myobj.mrlCode);
                //数量
                $("#qty").val(myobj.qty);
                //批次号
                $("#lotCode").val(myobj.lotCode);
                //检测状态下拉框
                var gyStatusList = eval(myobj.gyStatusList);
                var html = "";
                for (var i = 0; i < gyStatusList.length; i++) {
                    html += "<option value='" + gyStatusList[i].code + "'>" + gyStatusList[i].name + "</option>";
                }
                $api.html($api.byId("gyStatus"), html);
                //状态赋值
                $("#gyStatus").val(flag);

                //检测结果下拉框
                var gyCkeckList = eval(myobj.gyCkeckList);
                var html = "<option value=''>--请选择--</option>";
                for (var i = 0; i < gyCkeckList.length; i++) {
                    if (gyCkeckList[i].code != "0") {
                        html += "<option value='" + gyCkeckList[i].code + "'>" + gyCkeckList[i].name + "</option>";
                    }
                }
                $api.html($api.byId("gyCheck"), html);

                //检测结果赋值
                $("#gyCheck").val(myobj.gyCheck);
                //新箱号
                $("#newBoxnum").val("");

                //设置状态
                setDisabled(flag, myobj.gyCheck);
                    //失败
                } else if (myobj.code == "1") {
                    api.toast({
                        msg: myobj.message,
                        duration: 3000
                    });
                }

                // //id赋值
                // $("#id").val(id);
                // //发货单号
                // $("#consignment").val(myobj.consignment);
                // //箱号
                // $("#boxnum").val(myobj.boxnum);
                // //物料号
                // $("#mrlCode").val(myobj.mrlCode);
                // //数量
                // $("#qty").val(myobj.qty);
                // //批次号
                // $("#lotCode").val(myobj.lotCode);
                // //检测状态下拉框
                // var gyStatusList = eval(myobj.gyStatusList);
                // var html = "";
                // for (var i = 0; i < gyStatusList.length; i++) {
                //     html += "<option value='" + gyStatusList[i].code + "'>" + gyStatusList[i].name + "</option>";
                // }
                // $api.html($api.byId("gyStatus"), html);
                // //状态赋值
                // $("#gyStatus").val(flag);

                // //检测结果下拉框
                // var gyCkeckList = eval(myobj.gyCkeckList);
                // var html = "<option value=''>--请选择--</option>";
                // for (var i = 0; i < gyCkeckList.length; i++) {
                //     if (gyCkeckList[i].code != "0") {
                //         html += "<option value='" + gyCkeckList[i].code + "'>" + gyCkeckList[i].name + "</option>";
                //     }
                // }
                // $api.html($api.byId("gyCheck"), html);

                // //检测结果赋值
                // $("#gyCheck").val(myobj.gyCheck);
                // //新箱号
                // $("#newBoxnum").val("");

                // //设置状态
                // setDisabled(flag, myobj.gyCheck);
            }
        });
    }

    //设置状态
    function setDisabled(gyStatus, gyCheck) {
        //放行（3）
        if (gyStatus == "3") {

            //光洋验证结果为空，都不可以编辑
            if (gyCheck == "" || gyCheck == null || gyCheck == undefined) {
                $("#qty").attr('disabled', true);
                $("#lotCode").attr('disabled', true);

                //数量不一致（1）
            } else if (gyCheck == "1") {
                $("#qty").attr('disabled', false);
                $("#lotCode").attr('disabled', true);

                //批次号不一致(2)
            } else if (gyCheck == "2" || gyCheck == "4") {
                $("#qty").attr('disabled', true);
                $("#lotCode").attr('disabled', false);

                //箱码不一致
            } else if (gyCheck == "3") {
                $("#qty").attr('disabled', true);
                $("#lotCode").attr('disabled', true);
            }

            //退货（4）
        } else if (gyStatus == "4") {
            $("#qty").attr('disabled', true);
            $("#lotCode").attr('disabled', true);
        }
    }

    //检测结果下拉框值改变
    function onChangeGyCheck() {
        //取下拉框值
        var gyCheck = $("#gyCheck").val();
        var gyStatus = $("#gyStatus").val();
        //设置是否可以bian
        setDisabled(gyStatus, gyCheck);
    }

    //扫码新箱号
    function toScanner() {
        var moduleScanner = api.require('moduleScanner');
        var params = {
            top: 150, //扫描窗口的上边距
            width: 250, //扫描窗口的宽
            height: 250, //扫描窗口的高
            useColor: false, //是否使用自己设定的颜色(startColor和endColor),
            //如果不使用设定的颜色就使用默认的那张网格图
            startColor: '#0F08C8FF', //渐变起始颜色
            endColor: '#7F08C8FF', //渐变结束颜色
            animTime: 2200, //扫描动画时间,单位为毫秒
        };
        moduleScanner.toScannerActivityForResult(params, function (ret, err) {
            var sm = ret;

            //新箱号：需要解析
            var text = document.getElementById("newBoxnum");
            var arr = sm.split(",");
            text.value = arr[9];

            //隐藏条码
            var textTM = document.getElementById("tm");
            textTM.value = sm;

        });
    }

    //替换箱码按钮
    function replace() {
        //取检测状态
        var gyStatus = $("#gyStatus").val();
        var gyCheck = $("#gyCheck").val();
        //放行（3）需要验证
        if (gyStatus == "3") {

            //检测结果不为箱码不一致，不能点击
            if (gyCheck != "3") {
                api.toast({
                    msg: '检测结果为箱码不一致才可以替换箱码！',
                    duration: 3000
                });
                return;
            }
            $("#newBoxnum").attr('disabled', false);
            // $("#scanner").attr('display', "block");

        } else if (gyStatus == "4") {
            api.toast({
                msg: '检测状态为退回，不能替换箱码！',
                duration: 3000
            });
            return;
        }
    }

    //保存
    function save() {
        //取ID
        var id = $("#id").val();
        //取检测状态
        var gyStatus = $("#gyStatus").val();
        //取检测结果
        var gyCheck = $("#gyCheck").val();
        //数量
        var qty = $("#qty").val();
        //批次号
        var lotCode = $("#lotCode").val();
        //箱号
        var newBoxnum = $("#newBoxnum").val();
        //新条码
        var tm = $("#tm").val();

        //放行（3）需要验证
        if (gyStatus == "3") {

            //检测结果
            //数量不一致（1）
            if (gyCheck == "1") {

                //数量不能为空
                if (qty == "" || qty == null || qty == undefined) {
                    api.toast({
                        msg: '数量不能为空！',
                        duration: 3000
                    });
                    return;
                }

                //数量必须大于0
                if (parseFloat(qty) <= 0) {
                    api.toast({
                        msg: '数量必须大于0！',
                        duration: 3000
                    });
                    return;
                }

                //批次号不一致（2）；批次号不符合先进先出（4）
            } else if (gyCheck == "2" || gyCheck == "4") {

                //批次号不能为空
                if (lotCode == "" || lotCode == null || lotCode == undefined) {
                    api.toast({
                        msg: '批次号不能为空！',
                        duration: 3000
                    });
                    return;
                }

                //箱码不一致
            } else if (gyCheck == "3") {

                //箱号不能为空
                if (newBoxnum == "" || newBoxnum == null || newBoxnum == undefined) {
                    api.toast({
                        msg: '新箱号不能为空！',
                        duration: 3000
                    });
                    return;
                }
                var arr = tm.split(",");
                var newMtlCode = arr[1];
                var mrlCode = $("#mrlCode").val();

                //两次物料号
                if (mrlCode != newMtlCode) {
                    api.toast({
                        msg: '新箱号物料号与系统箱物料号不一致！',
                        duration: 3000
                    });
                    return;
                }

                //检测结果为空
            } else if (gyCheck == "" || gyCheck == null || gyCheck == undefined) {
                api.toast({
                    msg: '请选中检测结果！',
                    duration: 3000
                });
                return;
            }
        }
        api.showProgress({
            title: '保存中...',
        });
        var un = localStorage.getItem('un');
        var serverUrl = localStorage.getItem('serverUrl');
        var url = serverUrl + "/app/getUpdateTMContent";
        var data = {
            values: {
                id: id,
                gyStatus: gyStatus,
                gyCheck: gyCheck,
                appVersion: api.appVersion,
                qty: qty,
                lotCode: lotCode,
                tm: tm,
                un: un
            }
        };
        api.ajax({
            url: url,
            method: 'post',
            dataType: 'json',
            timeout: 30,
            returnAll: false,
            data: data
        }, function (ret, err) {
            if (ret) {
                var data = JSON.stringify(ret);
                var myobj = eval("(" + data + ")");
                //成功
                if (myobj.code == "0") {
                    api.hideProgress();
                    api.toast({
                        msg: "保存成功",
                        duration: 3000
                    });

                    var consignment = $("#consignment").val();
                    api.closeWin({});
                    api.execScript({
                        name: api.pageParam.name,
                        script: 'refresh("' + consignment + '")'
                    });

                    //失败
                } else if (myobj.code == "1") {
                    api.hideProgress();
                    api.toast({
                        msg: myobj.message,
                        duration: 5000
                    });
                }
            }
        });
    }

    //箱号监听回车事件
    $("#newBoxnum").on('keypress', function (e) { //#keyword为input文本框
        var keycode = e.keyCode;
        var tm = $(this).val();
        if (keycode == '13') {

            //条码解析
            var arr = tm.split(",");
            //箱号
            $("#newBoxnum").val(arr[9]);
            //条码
            $("#tm").val(tm);

            //发货单选中
            $("#newBoxnum").select();
        }
    });

    //返回按钮
    function closeWin() {
        api.closeWin({});
    }
</script>

</html>